# -*- coding: UTF-8 -*-
"""
文字预处理脚本
传入html
输出公示与公告的预处理数据

"""
import re
import sys
from TextTwo import Texttwo
from loguru import logger

class Text():
    def __init__(self,**params1):
        self.html = ""
        self.result = []
        self.winner = []
        self.money = []
        self.manage = []
        self.tender = []
        self.clear_solo = params1['clear_solo']
        self.config = params1['config']
        self.data_source_id = params1['data_source_id']

    def html_replace(self,html,replace_keyword,find_key_replace):

        for i in replace_keyword:
            html = re.sub(i[0], i[1], html, flags=re.S | re.I | re.VERBOSE)
        if find_key_replace == '':
            return html

        for i in find_key_replace:
            obj = re.search(i[0], html, re.S | re.I)
            if obj:
                html = re.sub(i[1], i[2], html, flags=re.S | re.I | re.VERBOSE)

        return html

    def read(self,html,database):
        self.result = []
        if database == True:
            self.html = html
        else:
            self.html = open(html, encoding='utf-8').read()
        self.html = re.split('class=".{0,10}footer', self.html)
        if len(self.html) > 1:
            if re.search('资格|要求|联系',self.html[0]):
                self.html = self.html[0]
            else:
                self.html = self.html[1]
        else:
            self.html = self.html[0]
        js_ls = []
        obj = re.findall('<script.{0,30}?>(.*?)</script>',self.html,re.I|re.M|re.S)
        for _ in obj:
            _ = _.replace('&lt;','<').replace('&gt;','>')
            obj1 = re.findall('<p.*?>(.*?)</p>',_)
            for __ in obj1:
                js_ls.append(__)
        for __ in js_ls:
            self.html += __
        self.html = self.clear_solo(self.html, self.config['dispose_solo_text_dict_before'], self.data_source_id)
        re_ls_before = self.config['re_ls_before']
        for i in re_ls_before:
            try:
                self.html = re.sub(i[0], i[1], self.html, re.S | re.I | re.M)
            except Exception as e:
                print(i)
                print('re wrong..................')
        self.html = self.html.replace('&lt;','<').replace('&gt;','>')
        for i in self.config['replace_label']:
            self.html = re.sub(i[0], i[1], self.html, flags=re.S | re.I)
        for i in self.config['replace_punctuation']:
            self.html = re.sub(i[0], i[1], self.html, flags=re.S | re.I)
        html_replace_one = self.clear_solo(self.html,self.config['dispose_solo_text_dict'],self.data_source_id)
        html_replace_one = self.html_replace(html_replace_one,self.config['replace_keyword_one'],self.config['find_key_replace_one'])
        # html_replace_one = html_replace_one.replace('项目负责人及资格','项目负责人')
        html_replace_two = self.html
        return html_replace_one,html_replace_two


if __name__ == '__main__':
    print(Text_Project_Data().read('1'))